/**
 * 
 */
package com.chee.plant.hibernate.dao;

import java.util.Calendar;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.chee.plant.hibernate.object.PlantLog;
import com.chee.plant.server.HibernateSessionFactory;

/**
 * 对应所有PlantLogDAO的数据库操作
 * 
 * @author xue cb
 * 
 */
public class PlantLogDAO extends BaseHibernateDAO {

	/**
	 * 根据用户Id得到用户日志
	 * 
	 * @param userId
	 *            用户Id
	 * @return 用户日志列表
	 */
	@SuppressWarnings("unchecked")
	public List<PlantLog> getLogByUserId(Integer userId) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select log from PlantLog as log where log.UserId=:userId";
		Query query = session.createQuery(hql);
		query.setInteger("userId", userId);
		return query.list();
	}

	/**
	 * 主方法，用于测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 1， 测试getLogByUserId(Integer userId)
		List<PlantLog> list = new PlantLogDAO().getLogByUserId(2);
		System.out.println(list.size());
		for (int i = 0; i < list.size(); i++) {
			System.out.println(((PlantLog) (list.get(i))).getCreateTime());
		}

		// 2, 测试 save(PlantLog log)
		PlantLog log = new PlantLog();
		log.setCreateTime(Calendar.getInstance().getTime());
		log.setDescription("卖出了5个企鹅！");
		log.setUserId(2);
		new PlantLogDAO().save(log);
	}

}
